//
//  RelaxationTherapy.swift
//  HIextractLicve
//
//  Created by HIextractLicve on 2024/12/4.
//

import UIKit
import MJRefresh
import ProgressHUD

class RelaxationTherapy: HTLCBasecompression {
    var hreingpickTager:Int = 0
    var eachVioViewHTLC:THLCNOrBUTTOn?
    var mindfulnessPageHTL:THLCNPageControll?
    var journeyHTLC:UIView?
    var mindfulness:Set<Int>?
    var suggestion:Set<String>?
    var  professionals:Set<CGFloat>?
    var exchangeHTLC:[String:Array<Dictionary<String,String>>]?
    var maingkingNOwRoomDAtaHTLC:Array<Dictionary<String,String>>{
      
       
        if hreingpickTager == 0 {
            
            
            return HTLCAllDealRelaxing.Relaxing.DYMTrendDataHTL.0
        }else  if hreingpickTager == 1 {
            
            return HTLCAllDealRelaxing.Relaxing.DYMForyouDataHTL.0
        }else  if hreingpickTager == 2 {
            return HTLCAllDealRelaxing.Relaxing.focussuuserHTL.0.filter { dict in
                let customized = "1customized0".components(separatedBy: "customized")
                return dict["HTLisliveuser"] == customized[1]
            }
        }else  {
            
           
            return HTLCAllDealRelaxing.Relaxing.DYMTrendDataHTL.0
        }
    }
    
    lazy var createbujiHTL = UIButton.init()
    lazy var trendButtonHTLC: UIButton = {
        let burrton = UIButton.init()
        burrton.setBackgroundImage(UIImage.init(named: "unselAIbg"), for: .normal)
        burrton.setBackgroundImage(UIImage.init(named: "unselredAIbg"), for: .selected)
        burrton.setTitle("Trending", for: .normal)
        burrton.tag = 10
        burrton.addTarget(self, action: #selector(targe_GettchatTHLCClick(sendHtlcr:)), for: .touchUpInside)
        burrton.setTitleColor(.white, for: .normal)
        burrton.titleLabel?.font = UIFont(name: "Trebuchet MS Bold", size: 15)
        return burrton
    }()
    
    lazy var forrryouButtonHTLC: UIButton = {
        let burrton = UIButton.init()
        burrton.setBackgroundImage(UIImage.init(named: "unselAIbg"), for: .normal)
        burrton.setBackgroundImage(UIImage.init(named: "unselredAIbg"), for: .selected)
        burrton.setTitle("For you", for: .normal)
        burrton.tag = 11
        burrton.addTarget(self, action: #selector(targe_GettchatTHLCClick(sendHtlcr:)), for: .touchUpInside)
        burrton.setTitleColor(.white, for: .normal)
        burrton.titleLabel?.font = UIFont(name: "Trebuchet MS Bold", size: 15)
        return burrton
    }()
    
    lazy var mfololllButtonHTLC: UIButton = {
        let burrton = UIButton.init()
        burrton.setBackgroundImage(UIImage.init(named: "unselAIbg"), for: .normal)
        burrton.setBackgroundImage(UIImage.init(named: "unselredAIbg"), for: .selected)
        burrton.setTitle("Following", for: .normal)
        burrton.tag = 12
        burrton.addTarget(self, action: #selector(targe_GettchatTHLCClick(sendHtlcr:)), for: .touchUpInside)
        burrton.setTitleColor(.white, for: .normal)
        burrton.titleLabel?.font = UIFont(name: "Trebuchet MS Bold", size: 15)
        return burrton
    }()
    
    
    lazy var builCollectionLiveViewHTL: UICollectionView = {
        let coijfLayout = UICollectionViewFlowLayout.init()
        coijfLayout.itemSize = CGSize.init(width: (UIScreen.main.bounds.width - 24.xHTL), height:320)
        
        coijfLayout.minimumInteritemSpacing = 12
        coijfLayout.scrollDirection = .vertical
        coijfLayout.minimumLineSpacing = 24
        
        
        let fiettCol = UICollectionView.init(frame: .zero, collectionViewLayout: coijfLayout)
        let nib = UINib(nibName: "MindfulnessPracticeLL", bundle: nil)
               
        fiettCol.register(nib, forCellWithReuseIdentifier: "HTLRDymmmlostViewCellID")
             
        fiettCol.delegate = self
        fiettCol.dataSource = self
        
        fiettCol.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 310, right: 0)
       fiettCol.showsVerticalScrollIndicator = false
        
            fiettCol.showsVerticalScrollIndicator = false
       
        fiettCol.backgroundColor = .clear
        fiettCol.dataSource = self
        
        fiettCol.mj_header = MJRefreshNormalHeader(refreshingBlock: {
          

            DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                fiettCol.reloadData()
                
                
                    fiettCol.mj_header?.endRefreshing()
                
                
            }
            
        })
        return fiettCol
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.builCollectionLiveViewHTL.reloadData()
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        trendButtonHTLC.isSelected = true
        NotificationCenter.default.addObserver(self, selector: #selector(bloaaalackSomentoneRemoveHTLC), name: NSNotification.Name("htldeleteuser"), object: nil)
        let kopImgf = UIImageView.init(image: UIImage.init(named: "CommunitytitleHTl"))
        kopImgf.contentMode = .scaleToFill
        view.addSubview(kopImgf)
        kopImgf.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(saaffeeTppoHightHTL + 14.yHTL)
            make.leading.equalToSuperview().offset(16.xHTL)
            make.width.equalTo(208.xHTL)
            make.height.equalTo(55.xHTL)
        }
        
        let nortyiButton = UIButton.init()
        nortyiButton.setImage(UIImage.init(named: "notify_hTL"), for: .normal)
        nortyiButton.addTarget(self, action: #selector(gotoNotiMeaassgeHTLC), for: .touchUpInside)
        view.addSubview(nortyiButton)
        nortyiButton.snp.makeConstraints { make in
            make.centerY.equalTo(kopImgf)
            make.trailing.equalToSuperview().inset(12.xHTL)
            make.width.equalTo(38.xHTL)
            make.height.equalTo(38.xHTL)
        }
        
        let comye = (UIScreen.main.bounds.width - 12.xHTL*4)/3
        if self.navigationController?.navigationBar.isHidden == true {
            journeyHTLC?.alpha = 1
            journeyHTLC?.layer.cornerRadius = 12
        }
        journeyHTLC?.backgroundColor = UIColor(red: 0.06, green: 0.07, blue: 0.07, alpha: 0.01)

        eachVioViewHTLC = THLCNOrBUTTOn.init()
        eachVioViewHTLC?.setTitle("anchors", for: .normal)
        if journeyHTLC != nil {
            journeyHTLC?.addSubview(eachVioViewHTLC ?? THLCNOrBUTTOn.init())
            view.addSubview(journeyHTLC!)
        }

        mindfulnessPageHTL = THLCNPageControll()
        mindfulnessPageHTL?.numberOfPages = 1
        mindfulnessPageHTL?.backgroundColor = UIColor(red: 0.06, green: 0.07, blue: 0.07, alpha: 0.01)
        mindfulnessPageHTL?.tintColor = UIColor(red: 0.06, green: 0.07, blue: 0.07, alpha: 0.01)
        mindfulnessPageHTL?.sizeToFit()

        if journeyHTLC != nil {
            journeyHTLC?.addSubview(mindfulnessPageHTL ?? THLCNPageControll.init())
           
        }


       
        view.addSubview(trendButtonHTLC)
        view.addSubview(forrryouButtonHTLC)
        view.addSubview(mfololllButtonHTLC)
        trendButtonHTLC.snp.makeConstraints { make in
            make.width.equalTo(comye)
            make.leading.equalToSuperview().offset(12.xHTL)
            make.height.equalTo(38)
            make.top.equalTo(kopImgf.snp.bottom).offset(16.xHTL)
        }
        
        forrryouButtonHTLC.snp.makeConstraints { make in
            make.width.equalTo(comye)
            make.centerX.equalToSuperview()
            make.height.equalTo(38)
            make.top.equalTo(kopImgf.snp.bottom).offset(16.xHTL)
        }
        
        mfololllButtonHTLC.snp.makeConstraints { make in
            make.width.equalTo(comye)
            make.trailing.equalToSuperview().inset(12.xHTL)
            make.height.equalTo(38)
            make.top.equalTo(kopImgf.snp.bottom).offset(16.xHTL)
        }
        createbujiHTL.setImage(UIImage.init(named: "homeTlepub"), for: .normal)
        createbujiHTL.addTarget(self, action: #selector(setuppostloviewHTL), for: .touchUpInside)
       
        
        ProgressHUD.animate()
        DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
            ProgressHUD.dismiss()
            ///
           
            
            self.view.addSubview(self.builCollectionLiveViewHTL)
            self.builCollectionLiveViewHTL.snp.makeConstraints { make in
                make.leading.trailing.equalToSuperview().inset(12)
                make.bottom.equalToSuperview()
                make.top.equalTo(self.trendButtonHTLC.snp.bottom).offset(20.xHTL)
            }
            
            self.view.addSubview(self.createbujiHTL)
            self.createbujiHTL.snp.makeConstraints { make in
                make.width.height.equalTo(58)
                make.right.equalToSuperview().inset(10)
                make.bottom.equalToSuperview().offset(-(self.tabBarController?.tabBar.frame.height ?? 90) - 12)
            }
        }
        
       
        
    }
    
    @objc func gotoNotiMeaassgeHTLC()  {
        mindfulness = [99]
        suggestion = ["decompression"]
        professionals = [12.1]
        exchangeHTLC = ["themtopicHTLC":[["decompression":"098"]]]
        self.navigationController?.pushViewController(EbanotionalResonance.init(), animated: true)
     }
    @objc func setuppostloviewHTL() {
        mindfulness = [99]
        suggestion = ["decompression"]
        professionals = [12.1]
        exchangeHTLC = ["themtopicHTLC":[["decompression":"098"]]]
        let ponew = PositivePsychology.init()
        self.navigationController?.pushViewController(ponew, animated: true)
//        let nerwVC = UINavigationController(rootViewController: ponew)
//        nerwVC.modalPresentationStyle = .overCurrentContext
//        nerwVC.navigationBar.isHidden = true
//        self.present(nerwVC, animated: true)
    }

    @objc func gogonextTonmesglistHTL(hijkBUtton:UIButton)  {
      
       
        if HTLCAllDealRelaxing.Relaxing.setChatAiHTL.aiChatNameHTL == nil {
           
            let Hcagtg = TeherapeuticTouch.init()
            
            self.navigationController?.pushViewController(Hcagtg, animated: true)
            
            
        }else{
            let Hcagtg = StorytellingHealing.init()
            
            self.navigationController?.pushViewController(Hcagtg, animated: true)
            
            
        }
    }
    
    
    @objc func bloaaalackSomentoneRemoveHTLC()  {
        mindfulness = [99]
        suggestion = ["decompression"]
        professionals = [12.1]
        exchangeHTLC = ["themtopicHTLC":[["decompression":"098"]]]
        builCollectionLiveViewHTL.reloadData()
    }
    
    
    @objc func targe_GettchatTHLCClick(sendHtlcr: UIButton) {
      
          
        if sendHtlcr.tag == 10 {
            trendButtonHTLC.isSelected = true
            forrryouButtonHTLC.isSelected = false
            mfololllButtonHTLC.isSelected = false
           
            
        }
        
        if sendHtlcr.tag == 11 {
            trendButtonHTLC.isSelected = false
            forrryouButtonHTLC.isSelected = true
            mfololllButtonHTLC.isSelected = false
        }
        
        if sendHtlcr.tag == 12 {
            trendButtonHTLC.isSelected = false
            forrryouButtonHTLC.isSelected = false
            mfololllButtonHTLC.isSelected = true
            
        }
        
        hreingpickTager = sendHtlcr.tag - 10
        
        self.builCollectionLiveViewHTL.mj_header?.beginRefreshing()
        
    }
}


extension RelaxationTherapy:UICollectionViewDelegate,UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return maingkingNOwRoomDAtaHTLC.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let passonHtlacData = maingkingNOwRoomDAtaHTLC[indexPath.row]
        
        let fffuarcell = collectionView.dequeueReusableCell(withReuseIdentifier: "HTLRDymmmlostViewCellID", for: indexPath) as! MindfulnessPracticeLL
        let Timrhg = (passonHtlacData["HTLLiveDYMPiclist"]  ?? "").components(separatedBy: "*")
        let customized = "1customized0".components(separatedBy: "customized")
        fffuarcell.oneCupCoverHTLC.image = UIImage(named: Timrhg.first  ?? "")
        fffuarcell.userHeaderHTL.image = UIImage(named: passonHtlacData["HTLCAvater"]  ?? "")
        fffuarcell.usernamhrHTl.text = passonHtlacData["HTLCkiName"]
        fffuarcell.pubtimewHTl.text = passonHtlacData["HTLDYMPunoTime"]
        fffuarcell.attitudeImageHTl.image = UIImage(named: (passonHtlacData["HTLCIDIlikeThisDIM"] == customized[0]) ? "dymmeiYIzan" : "dymmeizan")
        fffuarcell.dyyumTitlewHTl.text = passonHtlacData["HTLLiveDYMDesc"]
        return fffuarcell
        
        
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let passonHtlacData = maingkingNOwRoomDAtaHTLC[indexPath.row]
       
        let enterlive = DeepBreathing.init(stressdescHTL: [233,45,677], passonHtlacData: passonHtlacData)
        self.navigationController?.pushViewController(enterlive, animated: true)
    }
    
    
}
